<template>
  <div>
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <van-field
          v-model="password"
          type="password"
          @change="valueTest"
          name="password"
          placeholder="新密码"
          :rules="[{ validator:validatorRegular, message: '请输入正确的密码' }]"
        />
        <van-field
          v-model="passwordQ"
          type="password"
          @change="valueTest"
          name="passwordQ"
          placeholder="再次输入新密码"
          :rules="[{ validator, message: '两次填写的密码不一致' }]"
        />
      </van-cell-group>
      <div style="margin: 16px">
        <van-button round block type="primary" :disabled="isDisabled" native-type="submit">
          提交
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { Toast } from 'vant';
export default {
  data() {
    return {
      password: "",
      passwordQ: "",
      isDisabled:true,
    };
  },
  methods:{
    validatorRegular(){
      return /^[0-9a-zA-Z]{6,16}$/.test(this.password)
    },
    validator(){
      if(this.password==this.passwordQ){
        // console.log(this.password==this.passwordQ);
        return true;
      }else{
        return false
      }
    },
     //两次密码检测一致
    valueTest(){
      if(this.password==this.passwordQ&& this.password.length>5){
        this.isDisabled=false;
        // console.log('通过 ',this.password.length);
      }else {
        // console.log('未通过 ')
        this.isDisabled=true;
      }
    },
    onSubmit(values){
      // console.log("submit", values);
      // console.log(this.validator());
      let Consistent=this.validator()
      // console.log(Consistent);
        // console.log('密码一致',this.$route.query.phone);
      
      if(Consistent){
        // console.log(this.$route.query.phone,this.passwordQ);
        this.$http({
          url:'/driver/updatePwd',
          method:'POST',
          params:{
            phone:this.$route.query.phone,
            pwd:this.passwordQ,
          }
        })
        .then(res=>{
          // console.log('请求成功',res);
          if(res.data.code==1){
            Toast(res.data.msg)
            setTimeout(()=>{
              this.$router.push('/')
            },1000)
          }else {
             Toast('修改失败')
          }
        })
        .catch(err=>{
          Toast('网络请求超时')
          // console.log('请求失败',err);
        })
      }
    },
  },
 
};
</script>